package org.alvin;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;

public class MCTJsonMain {

    public static void main(String[] args) throws IOException {
        String [] jobnos =new String []{"IPQC20220805005","IPQC20220801001","IPQC20220805009","IPQC20220721004","IPQC20220729012","IPQC20220805010","IPQC20220624001"};
        byte[] buff = Files.readAllBytes(Paths.get("F:", "sys_kafka_log_202208171052.json"));
        JSONObject jsonObject = JSON.parseObject(new String(buff));
        JSONArray list = jsonObject.getJSONArray("sys_kafka_log");
        JSONObject res = new JSONObject();
        for (int i = 0; i < list.size(); i++) {
            JSONObject json = list.getJSONObject(i);
            JSONObject data = json.getJSONObject("data").getJSONObject("data");
            JSONObject item = new JSONObject();
            item.put("jobno", data.getJSONObject("dynProcess").getString("jobno"));
            item.put("createTime", data.getJSONObject("dynProcess").getString("created_time"));
            item.put("data", data);
            if(res.getJSONObject(data.getJSONObject("dynProcess").getString("jobno")) != null){
                JSONObject tmp = res.getJSONObject(data.getJSONObject("dynProcess").getString("jobno"));
                long time = tmp.getLong("createTime");
                long src = item.getLong("createTime");
                if(src > time){
                    if(!exist(jobnos,data.getJSONObject("dynProcess").getString("jobno"))){
                        res.put(data.getJSONObject("dynProcess").getString("jobno"), item);
                    }

                }
            }else {
                if(!exist(jobnos,data.getJSONObject("dynProcess").getString("jobno"))){
                    res.put(data.getJSONObject("dynProcess").getString("jobno"), item);
                }
            }
        }
        System.out.println(res.size());
        System.out.println(res.toJSONString());
        System.out.println(res.keySet());


    }

    private static boolean exist(String[] jobnos, String jobno) {
        for(String job : jobnos){
            if(job.equals(jobno)){
                return true;
            }
        }
        return false;
    }
}
